package com.linkage.educloud.ah.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.bean.AppDetail;

import java.util.List;

/**
 * Created by Mr'Tang on 2018/12/24.
 */

public class AppDetailChargesAdapter extends android.widget.BaseAdapter {

    private Context mContext;
    private List<AppDetail> mList;

    private int arg = 0;

    public AppDetailChargesAdapter(Context context, List<AppDetail> list) {
        this.mContext = context;
        this.mList = list;
    }

    public void update(int a) {
        arg = a;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public AppDetail getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_appdetail_charges, null);
            viewHolder = new ViewHolder();
            viewHolder.item_charges_radio = convertView.findViewById(R.id.item_charges_radio);
            viewHolder.item_charges_name = convertView.findViewById(R.id.item_charges_name);
            viewHolder.item_charges_price = convertView.findViewById(R.id.item_charges_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        AppDetail info = getItem(position);
        viewHolder.item_charges_name.setText(info.getName());
        viewHolder.item_charges_price.setText(info.getPrice());

        if (arg == position) {
            viewHolder.item_charges_radio.setChecked(true);
        } else {
            viewHolder.item_charges_radio.setChecked(false);
        }
        return convertView;
    }

    private class ViewHolder {
        RadioButton item_charges_radio;
        TextView item_charges_name;
        TextView item_charges_price;
    }
}
